package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.Sign;
import cn.wolfcode.trip.base.mapper.SignMapper;
import cn.wolfcode.trip.base.service.ISignService;
import cn.wolfcode.trip.base.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author 不出bug- 么么哒
 * @DESCRIPTION
 * @create 2018/11/15
 */
@Service
public class SignServiceImpl implements ISignService {
    @Autowired
    private SignMapper signMapper;

    @Override
    public List<Sign> getSignsByUserId(Long userId) {
        return signMapper.selectByUser (userId, null);
    }

    @Override
    public Sign getTodaySignByUserId(Long userId) {
        List<Sign> signs = signMapper.selectByUser (userId, DateUtil.getFormatDate (new Date ()));
        if (signs.size () != 0) {
            return signs.get (0);
        }
        return null;
    }

    @Override
    public void saveSignInfo(Sign sign) {
        Date date = new Date ();
        Integer count = signMapper.selectCountBySignDate (DateUtil.getPreviousDate (date), sign.getUserId ());
        if (count == null) {
            sign.setCount (1);
        } else {
            sign.setCount (count + 1);
        }
        sign.setSignDate (date);
        signMapper.insert (sign);
        Integer points = signMapper.selectPointsByUserId (sign.getUserId ());
        if (points == null) {
            signMapper.insertPoints (sign.getUserId (), sign.getPoints ());
            return;
        }
        signMapper.updatePoints (sign.getUserId (), sign.getPoints () + points);
    }
}
